using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.POS
{
    public partial class guiSelectPromotionProducts : THSERPScreen
    {
        private List<ICPromotionItemDetailsInfo> PromotionItemDetailsList;

        public guiSelectPromotionProducts()
        {
            InitializeComponent();

            //Init lookup edit columns
            DataSet ds = (DataSet)THSApp.LookupTables[THSUtil.GetTableNameFromBusinessObjectType(typeof(ICProductsInfo))];
            if (ds.Tables.Count > 0)
                (colFK_ICProductID.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit).DataSource = ds.Tables[0];
        }

        public guiSelectPromotionProducts(List<ICPromotionItemDetailsInfo> lstICPromotionItemDetailsInfo)
        {
            InitializeComponent();

            //Init lookup edit columns
            DataSet ds = new ICProductsController().GetAllObjects();
            if (ds.Tables.Count > 0)
                (colFK_ICProductID.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit).DataSource = ds.Tables[0];

            PromotionItemDetailsList = lstICPromotionItemDetailsInfo;
            fld_dgcPromotionProducts.DataSource = PromotionItemDetailsList;
        }

        private void guiSelectPromotionProducts_Load(object sender, EventArgs e)
        {
            fld_dgcPromotionProducts.Focus();
            ((DevExpress.XtraGrid.Views.Grid.GridView)fld_dgcPromotionProducts.MainView).FocusedRowHandle = 0;
        }

        private void fld_btnOK_Click(object sender, EventArgs e)
        {
            POSEntities entity = (POSEntities)((BaseModuleERP)Module).CurrentModuleEntity;
            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)fld_dgcPromotionProducts.MainView;
            int[] arrSelectedRows = gridView.GetSelectedRows();

            for (int i = 0; i < arrSelectedRows.Length; i++)
            {
                ICPromotionItemDetailsInfo objPromotionItemDetailsInfo = PromotionItemDetailsList[gridView.GetDataSourceRowIndex(arrSelectedRows[i])];
                BRPOSItemsInfo objPOSItemsInfo = GetPOSItemFromPromotionItemDetail(objPromotionItemDetailsInfo);
                POSItemsEntity entItem = new POSItemsEntity();
                entItem.GetFromBusinessObject(objPOSItemsInfo);
                entity.BRPOSItemsList.Add(entItem);
            }
            
            entity.BRPOSItemsList.GridControl.RefreshDataSource();
            entity.BRPOSItemsList.GridViewFocusRow(entity.BRPOSItemsList.Count - 1);
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_dgcPromotionProducts_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                fld_btnOK_Click(new object(), new EventArgs());

                DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)fld_dgcPromotionProducts.MainView;
                if (gridView.FocusedRowHandle == gridView.RowCount - 1)
                    gridView.MoveFirst();
                else
                    gridView.MoveNext();
            }
            else 
                if (e.KeyCode == Keys.Escape)
                    this.Close();
        }

        public BRPOSItemsInfo GetPOSItemFromPromotionItemDetail(ICPromotionItemDetailsInfo objPromotionItemDetailsInfo)
        {
            BRPOSItemsInfo objPOSItemsInfo = new BRPOSItemsInfo();
            
            objPOSItemsInfo.FK_ICProductID = objPromotionItemDetailsInfo.FK_ICProductID;
            
            objPOSItemsInfo.BRPOSItemProductType = "ProductTypePromotion";
            
            //Set item quantity, unit price and price
            objPOSItemsInfo.BRPOSItemProductQty = objPromotionItemDetailsInfo.ICPromotionItemDetailQty;
            if (objPromotionItemDetailsInfo.ICPromotionItemDetailIncludeCheckPrice)
                objPOSItemsInfo.BRPOSItemProductUnitPrice = objPromotionItemDetailsInfo.ICPromotionItemDetailUnitPrice;
            else
                objPOSItemsInfo.BRPOSItemProductUnitPrice = 0;
            objPOSItemsInfo.BRPOSItemPrice = objPOSItemsInfo.BRPOSItemProductUnitPrice * objPOSItemsInfo.BRPOSItemProductQty;

            //Get item tax percent
            ICProductsInfo objProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(objPOSItemsInfo.FK_ICProductID);
            objPOSItemsInfo.BRPOSItemProductTaxPercent = 0;
            if (objProductsInfo != null)
            {
                GEVATsInfo objVATsInfo = (GEVATsInfo)new GEVATsController().GetObjectByID(objProductsInfo.FK_GEVATID);
                if (objVATsInfo != null)
                    objPOSItemsInfo.BRPOSItemProductTaxPercent = objVATsInfo.GEVATPercentValue;
            }            

            //Set item tax amount
            objPOSItemsInfo.BRPOSItemTaxAmount = objPOSItemsInfo.BRPOSItemPrice * objPOSItemsInfo.BRPOSItemProductTaxPercent;

            //Set item net amount
            objPOSItemsInfo.BRPOSItemNetAmount = objPOSItemsInfo.BRPOSItemPrice + objPOSItemsInfo.BRPOSItemTaxAmount;

            //Set item discount amount
            objPOSItemsInfo.BRPOSItemProductDiscount = 0;
            objPOSItemsInfo.BRPOSItemDiscountAmount = objPOSItemsInfo.BRPOSItemPrice * objPOSItemsInfo.BRPOSItemProductDiscount;
           
            //Set item total amount
            objPOSItemsInfo.BRPOSItemTotalAmount = objPOSItemsInfo.BRPOSItemNetAmount - objPOSItemsInfo.BRPOSItemDiscountAmount;

            return objPOSItemsInfo;
        }
    }
}